$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'noc/nocwenjuan/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '模板', name: 'tname', sortable: false, width: 50 },
			{ label: '字段', name: 'colName', sortable: false, width: 80 },
			{ label: '题目', name: 'title', index: 'title', width: 80 }, 			
			{ label: '类型 1:单选  2:多选', name: 'type', index: 'type', width: 80 }, 			
			{ label: '排序', name: 'sort', index: 'sort', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		nocWenjuan: {},
        templates:[],
        templateId: "",
        cols: [],
	},
    watch: {
        templateId (val,oldval){
            console.log(val);
            vm.nocWenjuan["tid"] = val;
            vm.getTemplateCols(val);
        }
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.nocWenjuan = {};
            vm.getTemplates();
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getTemplates();
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.nocWenjuan.id == null ? "noc/nocwenjuan/save" : "noc/nocwenjuan/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.nocWenjuan),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "noc/nocwenjuan/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "noc/nocwenjuan/info/"+id, function(r){
                vm.nocWenjuan = r.nocWenjuan;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        getTemplates: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "noc/noctemplate/list?limit=100",
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code == 0){
                        vm.templates = r.page.list;
                    }
                    //获取默认活动
                    vm.getDefaultTemplateId();
                }
            });
        },
        getDefaultTemplateId: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "noc/noctemplate/getDefaultTemplateId",
                contentType: "application/json",
                data: {},
                success: function(r){
                    console.log(r);
                    if(r.msg){
                        vm.templateId = r.msg;
                    }
                }
            });
        },
        getTemplateCols: function (tid) {
            $.ajax({
                type: "POST",
                url: baseURL + "noc/noctemplatecol/listByTid?templateId="+tid+"&limit=100",
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code == 0){
                        console.log(r.page.list)
                        vm.cols = r.page.list;
                    }
                }
            });
        }
	}
});